target:=happy-snake
object-dir:=obj
source-dir:=src
header-dir:=src

CC=g++
CFLAGS=-g -std=c++11
CPPFLAGS=
INCLUDES=-I $(header-dir)
COMPILE.cpp=$(CC) $(CFLAGS) $(CPPFLAGS) $(INCLUDES) -c

MKDIR=mkdir -p
RM=rm -rf

######################################################################
sources=$(wildcard $(source-dir)/*.cpp)
objects=$(patsubst $(source-dir)/%.cpp,$(object-dir)/%.o,$(sources))
depends=$(patsubst $(source-dir)/%.cpp,$(object-dir)/.d/%.d,$(sources))

.PHONY: clean all

all: $(target)

$(target): $(objects)
	$(CC) $^ -lpthread -o $@

-include $(depends)

$(depends): $(object-dir)/.d/%.d : $(source-dir)/%.cpp | $(object-dir)/.d
	$(COMPILE.cpp) -M $< > $@.$$$$; \
	sed 's,\(\<.*\):,\1 $@:,g' < $@.$$$$ > $@; \
	rm -f $@.$$$$

$(objects): $(object-dir)/%.o : $(source-dir)/%.cpp $(object-dir)/.d/%.d | $(object-dir)
	$(COMPILE.cpp) $< -o $@

$(object-dir):
	$(MKDIR) $@

$(object-dir)/.d:
	$(MKDIR) $@

clean:
	$(RM) $(object-dir) $(target)
